package com.jxnu.escms.Control.UserModule;


import com.github.pagehelper.Page;
import com.jxnu.escms.Control.UserControl.UserControl;
import com.jxnu.escms.Pojo.PageResult;
import com.jxnu.escms.Pojo.User;
import com.jxnu.escms.Service.UserService.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;

@Slf4j
@RestController
public class UserModule {

    @Resource
    private UserService userService;

    @Resource
    private UserControl userControl;

    @RequestMapping("/getall")
    public Map<String, Object> getUsers(@RequestBody Map<String, Object> page){

        Integer start = (Integer) page.get("start");
        Integer pageSize = (Integer) page.get("pageSize");
        PageResult<User> users = userService.getUsers(start, pageSize);


        Page<User> pages = users.getPages();
        for(User p: pages){
            // 获取地址
            String address = userControl.getAddress(p, p.getId());
            p.setAddress(address);
            if(address != null) {
                List addList = userControl.parseAddress(address);
                p.setComName((String) addList.get(0));
                p.setBuild((String) addList.get(1));
                p.setNuit((String) addList.get(2));
                p.setDoor((String) addList.get(3));
            }
            p.setHealthCode(p.getHealthCode() + "码");
        }

        Map<String, Object> res = new HashMap<>();
        res.put("res", users);
        return res;
    }

    @RequestMapping("/deleteUser")
    public Map deleteUser(@RequestBody Map<String, Object> idMap){
        Integer adminId = (Integer) idMap.get("admin_id");
        List<Integer> user_id = (List<Integer>) idMap.get("user_id");

        for (int p : user_id){
            userService.userCharge(adminId, p, "删除");
        }

        HashMap<String, String> res = new HashMap();
        res.put("delete", "success");
        return res;
    }

    @RequestMapping("/modify")
    public Map modify(@RequestBody User user){

        // 更新地址
        List<String> newList = new ArrayList<String>();
        String comName = user.getComName();
        String build = user.getBuild();
        String nuit = user.getNuit();
        String door = user.getDoor();
        if(comName == null && build == null && nuit == null && door == null){}
        else{
            // 获取原先地址
            List addList = userControl.parseAddress(userControl.getAddress(user, user.getId()));
            newList.add(comName != null? comName: (String) addList.get(0));
            newList.add(build != null? build: (String) addList.get(1));
            newList.add(nuit != null? nuit: (String) addList.get(2));
            newList.add(door != null? door: (String) addList.get(3));
            userService.updateUserAddress(newList, user.getId());
        }
        // 更新用户数据
        User userByID = userService.getUserByID(user.getId());
        user.setAccount(user.getAccount() == null ? userByID.getAccount(): user.getAccount());
        user.setRealName(user.getRealName() == null ? userByID.getRealName(): user.getRealName());
        user.setSex(user.getSex() == null ? userByID.getSex(): user.getSex());
        user.setAge(user.getAge() == null ? userByID.getAge(): user.getAge());
        user.setState(user.getState() == null ? userByID.getState(): user.getState());
        user.setRiskLevel(user.getRiskLevel() == null ? userByID.getRiskLevel(): user.getRiskLevel());
        user.setHealthCode(user.getHealthCode() == null ? userByID.getHealthCode():
                user.getHealthCode().replace("码",""));
        userService.updateUser(user);

        Map<String, String> res = new HashMap<>();
        res.put("modify", "success");
        return res;
    }

    protected boolean judgeIsOk(User user, int statue){
        // statue: 0: 代表住户管理； 1： 代表高危用户
        boolean flag = true;
        if(statue == 0){
            if (Objects.equals(user.getPower(), "管理员") ||
                    Objects.equals(user.getHealthCode(), "红"))
            {
                flag = false;
            }
        }
        if(statue == 1){
            if (Objects.equals(user.getPower(), "管理员") ||
                    !Objects.equals(user.getHealthCode(), "红"))
            {
                flag = false;
            }
        }
        return flag;
    }
    @RequestMapping("query")
    public Map queryUser(@RequestBody Map<String, String> args){
        String account = (String) args.get("account");
        String id = (String) args.get("id");
        String realName = (String) args.get("realName");
        int status = Integer.parseInt(args.get("status"));

        User userByID ;
        if(!Objects.equals(id, "")){
            userByID = userService.getUserByID(Integer.parseInt(id));
        }
        else{
            userByID = userService.getUserByAAndN(account, realName);
        }


        Map<String, Object> res = new HashMap<>();

        if(userByID != null){
            if(!judgeIsOk(userByID, status)){
                res.put("query", "worry");
                return res;
            }
            // 获取地址
            String address = userControl.getAddress(userByID, userByID.getId());
            userByID.setAddress(address);
            if(address != null) {
                List addList = userControl.parseAddress(address);
                userByID.setComName((String) addList.get(0));
                userByID.setBuild((String) addList.get(1));
                userByID.setNuit((String) addList.get(2));
                userByID.setDoor((String) addList.get(3));
            }
            userByID.setHealthCode(userByID.getHealthCode() + "码");
            res.put("query", "success");
            res.put("result", userByID);
        }
        else{
            res.put("query", "worry");
        }
        return res;
    }

    @RequestMapping("/addUser")
    public Map addUser(@RequestBody User user){
        System.out.println(user);
        Map<String, Object> res = new HashMap<>();
        User userByAAndN = userService.getUserByAAndN(user.getAccount(),"");
        if(userByAAndN != null){
            res.put("adduser", "worry");
            res.put("result", "当前用户已存在！！！");
        }
        else{
            user.setHealthCode(user.getHealthCode().replace("码", ""));
            int i = userService.addUser(user);
            if(i == 1){
                User user1 = userService.getUserByAAndN(user.getAccount(), user.getRealName());
                int id = user1.getId();
                String comName = user.getComName();
                // 判断是否存在com这个社区
                Map c = userService.containCom(comName);
                if(c == null){
                    // 添加社区
                    userService.insertCom(comName);
                    c = userService.containCom(comName);
                }

                // 添加地址
                userService.addAddress(id, (Integer) c.get("com_id"),
                        user.getBuild(), user.getNuit(), user.getDoor());
                res.put("add", "success");
            }
        }
        return res;
    }

    @RequestMapping("/dangerous")
    public Map getDangerous(@RequestBody Map<String, Object> page){
        Integer start = (Integer) page.get("start");
        Integer pageSize = (Integer) page.get("pageSize");
        PageResult<User> users = userService.getDangerUsers(start, pageSize);

        Page<User> pages = users.getPages();
        for(User p: pages){
            // 获取地址
            String address = userControl.getAddress(p, p.getId());
            p.setAddress(address);
            if(address != null) {
                List addList = userControl.parseAddress(address);
                p.setComName((String) addList.get(0));
                p.setBuild((String) addList.get(1));
                p.setNuit((String) addList.get(2));
                p.setDoor((String) addList.get(3));
            }
            p.setHealthCode(p.getHealthCode() + "码");
        }

        Map<String, Object> res = new HashMap<>();
        res.put("res", users);
        return res;
    }
}
